******* PART 1: DIESEL 


use "$path\Intermediary Data\localshock_dataforReg_v2.dta",clear

drop if prix_ht==.
drop j j3
xtset id daten
capture drop d_dieselr
gen d_dieselr=ln(diesel_rotterdam_euro)-ln(l1.diesel_rotterdam_euro)

drop diesel_rotterdam_euro resid_movav_diesel dprix1 

*** Step 1 ***
gen t=1
bysort id: egen nobs_stations=sum(t)
keep if nobs_stations>500
gen minus_number_obs=-nobs_stations
sort minus_number_obs id

gen test=(local_shock10==.)
bys id: egen st=sum(test)

gen test2=1
bys id: egen st2=sum(test2)
drop if (st2==st)
gen tt=st2-st

drop if tt<40
drop t test st test2 st2 tt

**** Step 2 : Iterate ****

egen id_select=group(minus_number_obs id)
drop minus_number_obs
drop if id_select==.


drop nobs_stations idb id_pdv

sum id_select, d
save "$path\Intermediary Data\data_forestimation_temp.dta", replace


forvalues iter=50 (50) 12300 {
local iterm=`iter'-50
clear
use "$path\Intermediary Data\data_forestimation_temp.dta", clear
keep if id_select>`iterm' & id_select<=`iter'

qui sum id_select, d
local rmax=r(max)
local rmin=r(min)


forvalue j = `rmin' (1) `rmax'  {

preserve
qui keep if id_select==`j'
capture drop num_obs
gen num_obs=_n




foreach i of numlist 0/40{
capture drop  f_prix_ht

qui bysort id (daten):  gen f_prix_ht=log(prix_ht[_n+`i'])-log(prix_ht[_n-1])

qui reg f_prix_ht d_dieselr local_shock10 m_dprix l.d_dieselr l2.d_dieselr l3.d_dieselr l4.d_dieselr l5.d_dieselr ///
		l.local_shock10  l2.local_shock10 l3.local_shock10 l4.local_shock10 l5.local_shock10 ///
	l.m_dprix  l2.m_dprix l3.m_dprix l4.m_dprix l5.m_dprix

qui gen coeff1_reg_`i'=_b[d_dieselr]
qui gen coeff2_reg_`i'=_b[local_shock10]

capture drop  f_prix_ht

}
quie duplicates drop id, force
keep coeff1_reg_*  coeff2_reg_* id

if `j'==1 {
save "$path\Intermediary Data\base_estim_EG_0821.dta", replace
}
if `j'>1 {
append using "$path\Intermediary Data\base_estim_EG_0821.dta"
save "$path\Intermediary Data\base_estim_EG_0821.dta", replace

}

restore
display "End of iteration number" `j'
qui drop if id_select==`j'
}
}

erase "$path\Intermediary Data\data_forestimation_temp.dta"

**** PART 2: MA


use "$path\Intermediary Data\localshock_dataforReg_v2.dta",clear

drop if prix_ht==.
drop  j j3
replace d_dieselr=resid_movav_diesel
drop diesel_rotterdam_euro resid_movav_diesel dprix1 
*** Step 1 ***

gen t=1
bysort id: egen nobs_stations=sum(t)
keep if nobs_stations>500

gen minus_number_obs=-nobs_stations
sort minus_number_obs id


gen test=(local_shock10==.)
bys id: egen st=sum(test)

gen test2=1
bys id: egen st2=sum(test2)
drop if (st2==st)
gen tt=st2-st

drop if tt<40
drop t test st test2 st2 tt

**** Step 2 : Iterate ****
egen id_select=group(minus_number_obs id)
drop minus_number_obs
drop if id_select==.


sum id_select, d
local rmax=r(max)

drop nobs_stations idb id_pdv
sum id_select, d
save "$path\Intermediary Data\data_forestimation_temp.dta"


*** Coupe la base en 50


forvalues iter=50 (50) 12300 {
local iterm=`iter'-50
clear
use "$path\Intermediary Data\data_forestimation_temp.dta", clear
keep if id_select>`iterm' & id_select<=`iter'

qui sum id_select, d
local rmax=r(max)
local rmin=r(min)


forvalue j = `rmin' (1) `rmax'  {

preserve
qui keep if id_select==`j'
capture drop num_obs
gen num_obs=_n
*



foreach i of numlist 0/40{
capture drop  f_prix_ht

qui bysort id (daten):  gen f_prix_ht=log(prix_ht[_n+`i'])-log(prix_ht[_n-1])

qui reg f_prix_ht d_dieselr local_shock10 m_dprix l.d_dieselr l2.d_dieselr l3.d_dieselr l4.d_dieselr l5.d_dieselr ///
		l.local_shock10  l2.local_shock10 l3.local_shock10 l4.local_shock10 l5.local_shock10 ///
	l.m_dprix  l2.m_dprix l3.m_dprix l4.m_dprix l5.m_dprix 

qui gen coeff1_reg_`i'=_b[d_dieselr]
qui gen coeff2_reg_`i'=_b[local_shock10]

capture drop  f_prix_ht

}
quie duplicates drop id, force
keep coeff1_reg_*  coeff2_reg_* id

if `j'==1 {
save "$path\Intermediary Data\base_estim_EG_MA.dta", replace
}
if `j'>1 {
append using "$path\Intermediary Data\base_estim_EG_MA.dta"
save "$path\Intermediary Data\base_estim_EG_MA.dta", replace

}

restore
display "End of iteration number" `j'
drop if id_select==`j'
}
}
erase "$path\Intermediary Data\data_forestimation_temp.dta"


****** Part 3: Brent *******

******************************************************************************
**********************************************************
************************************

**** Prerequisite 1: create a dataset with only brent data in euro ****

clear
use "$path\Intermediary Data\Brent_daily.dta"

merge 1:1 date_tokeep using "$path\Intermediary Data\eurusd_herokuapp_daily.dta"
drop _merge

gen brenteuro=brent/eurusd
gen date_maj_num=date_tokeep
keep date_maj_num brenteuro
drop if brenteuro==.

save "$path\Intermediary Data\DataReg_Brentonly.dta", replace

***** Prerequisite 2: create data with Brent for regressions ***

use "$path\Intermediary Data\localshock_dataforReg_brent.dta", clear
merge m:1 date_maj_num using "$path\Intermediary Data\DataReg_Brentonly.dta"
keep if _merge!=2


drop if j3==4
drop daten
egen daten=group(date_maj_num)
keep date_maj_num daten brenteuro
duplicates drop daten, force
save "$path\Intermediary Data\data_brent.dta", replace


/******** Brent Only ********/


clear all

use "$path\Intermediary Data\localshock_dataforReg_v2.dta",clear
merge m:1 daten using "$path\Intermediary Data\data_brent.dta"


xtset id daten
gen d_brenteuro=ln(brenteuro)-ln(l1.brenteuro)

drop if prix_ht==.
drop j j3


drop diesel_rotterdam_euro resid_movav_diesel dprix1 

*** Step 1 ***
gen t=1
bysort id: egen nobs_stations=sum(t)
keep if nobs_stations>500

gen minus_number_obs=-nobs_stations
sort minus_number_obs id

gen test=(local_shock10==.)
bys id: egen st=sum(test)

gen test2=1
bys id: egen st2=sum(test2)
drop if (st2==st)
gen tt=st2-st

drop if tt<40
drop t test st test2 st2 tt

**** Step 2 : Iterate ****
egen id_select=group(minus_number_obs id)
drop minus_number_obs
drop if id_select==.



sum id_select, d
local rmax=r(max)

drop nobs_stations idb id_pdv
sum id_select, d
save "$path\Intermediary Data\data_forestimation_temp.dta"



forvalues iter=50 (50) 12300 {
local iterm=`iter'-50
clear
use "$path\Intermediary Data\data_forestimation_temp.dta", clear
keep if id_select>`iterm' & id_select<=`iter'

qui sum id_select, d
local rmax=r(max)
local rmin=r(min)

forvalue j = `rmin' (1) `rmax'  {

preserve
qui keep if id_select==`j'
capture drop num_obs
gen num_obs=_n



foreach i of numlist 0/40{
capture drop  f_prix_ht

qui bysort id (daten):  gen f_prix_ht=log(prix_ht[_n+`i'])-log(prix_ht[_n-1])

qui reg f_prix_ht d_brenteuro l.d_brenteuro l2.d_brenteuro l3.d_brenteuro l4.d_brenteuro l5.d_brenteuro

qui gen coeff1_reg_`i'=_b[d_brenteuro]

capture drop  f_prix_ht

}
quie duplicates drop id, force
keep coeff1_reg_*   id

if `j'==1 {
save "$path\Intermediary Data\base_estim_EG_brent_only.dta", replace
}
if `j'>1 {
append using "$path\Intermediary Data\base_estim_EG_brent_only.dta"
save "$path\Intermediary Data\base_estim_EG_brent_only.dta", replace

}

restore
display "End of iteration number" `j'
drop if id_select==`j'
}
}

